<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="mvc" uri="http://www.springframework.org/tags/form" %>
<%@taglib prefix="fmt" uri="http://www.springframework.org/tags" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>申报计划管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=10,chrome=1">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
    <link rel="stylesheet" type="text/css" href="/lib/layui/layui/css/layui.css"/>
    <script type="text/javascript" src="/js/news/jquery-1.9.1.js"></script>
    <script src="/js/base/base.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../js/jquery/jquery.cookie.js"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.ui.widget.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.iframe-transport.js" type="text/javascript" charset="utf-8"></script>
    <script src="/lib/jQuery-File-Upload-master/jquery.fileupload.js" type="text/javascript" charset="utf-8"></script>
    <script type="text/javascript" src="/js/common/fileupload.js"></script>
    <script type="text/javascript" src="/lib/layui/layui/layui.js"></script>
    <script type="text/javascript" src="/lib/layui/layui/layui.all.js"></script>
</head>
<style>
    .mbox {
        padding: 8px
    }

    .item img {
        height: 40px;
    }

    .btn {
        height: 30px;
    }

    .btn button {
        float: right;
    }

    .information {
        background: #e8f4fc;
        height: 30px;
        line-height: 30px;
    }

    .add {
        height: 30px;
    }

    .layui-form-item {
        margin-bottom: 5px;
    }
    .layui-input-inline{
        width: 300px !important;
    }
    .openFile input[type=file]{
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 18px;
        z-index: 99;
        opacity: 0;
    }
    .layui-table-view i{
        width: 18px;
        height: 18px;
        margin-top: 5px;
    }
    .layui-form-label{
        width: 91px;
    }
</style>
<body>
<div class="mbox">
    <div class="item">
        <img src="../img/yeji.png" alt="" style="margin: 0 5px 0 20px;"> <span
            style="font-size: 22px;display: inline-block;vertical-align: middle;">申报计划管理</span>
    </div>
    <hr class="layui-bg-blue">
    <div class="btn">
        <button type="button" class="layui-btn layui-btn-sm layui-btn-normal export" style="margin-left: 20px"><i
                class="layui-icon layui-icon-upload-drag" style="vertical-align: middle;"></i>导出
        </button>
        <button type="button" class="layui-btn layui-btn-sm add" style="background: #2b7fe0"><i
                class="layui-icon layui-icon-addition"></i>新建
        </button>
    </div>
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>
</body>
</html>
<script type="text/html" id="barDemo">
<%--
    <a class="layui-btn layui-btn-xs" lay-event="declare">申报</a>
--%>
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
</script>
<script>
    layui.use(['table', 'form', 'laydate','upload'], function () {
        var table = layui.table,
            form = layui.form,
            laydate = layui.laydate,
            upload = layui.upload;

        var tableInt=table.render({
            elem: '#test'
            , url: '/rpmTopic/getPlanList'
            , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
            ,parseData: function(res){ //res 即为原始返回的数据
                return {
                    "code":0, //解析接口状态
                    "data": res.object, //解析数据列表，
                    "count": res.count, //解析数据长度
                };
            }
            , defaultToolbar: ['']
            , title: '用户数据表'
            , cols: [[
                {type: 'checkbox', fixed: 'left'}
                , {field: 'planName', title: '计划名称'}
                , {field: 'state', title: '计划状态',templet: function(d){
                        if(d.state=='1'){
                            return '申报中'
                        }else{
                            return '申报结束'
                        }
                    }}
                , {field: 'beginDate', title: '开始日期'}
                , {field: 'endDate', title: '结束日期',templet: function(d){
                        //当前
                        var timestamp = Date.parse(new Date());
                        var strtime = d.endDate;
                        var date = new Date(strtime.replace(/-/g, '/'));
                        //结束
                        time3 = Date.parse(date);
                       var planid = d.planId +','
                        console.log(planid)
                       if(timestamp - time3 >0){
                           $.ajax({
                               url: '/rpmTopic/updateRpmPlan',
                               data: {planId: planid},
                               dataType: 'json',
                               type: 'get',
                           })
                       }
                        return d.endDate
                    }
                }
                , {field: 'type', title: '操作', toolbar: '#barDemo'}
            ]]
            , page: true
        });
        //新建
        $('.add').click(function () {
            creat(0)
        });
        /*新建和编辑的方法*/
        function creat(type, planId) {
            if (type == '0') {
                var title = '新建';
            } else {
                var title = '编辑';
            }
            layer.open({
                type: 1,
                title: title,
                btn: ['确定','返回'],
                shade: 0.5,
                maxmin: true, //开启最大化最小化按钮
                area: ['40%', '80%'],
                content: '<div id="cont" style="margin: 10px">\n' +
                    '        <form class="layui-form" action="" id="ajaxforms" lay-filter="ajaxforms">\n' +
                    '            <h3 class="information">【基本信息】</h3>\n' +
                    '            <div class="layui-form-item" style="margin-top: 15px">\n' +
                    '                <div class="layui-inline">\n' +
                    '                   <span style="color:red">*</span><label class="layui-form-label">申报计划:</label>\n' +
                    '                    <div class="layui-input-inline">\n' +
                    '                        <input type="text" name="planName" lay-verify="required|phone" autocomplete="off"\n' +
                    '                               class="layui-input required">\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '            </div>\n' +
                    '            <div class="layui-form-item">\n' +
                    '                <div class="layui-inline">\n' +
                    '                    <label class="layui-form-label">项目来源</label>\n' +
                    '                    <div class="layui-input-inline">\n' +
                    '                        <select name="source"  class="source" lay-search="">\n' +
                    '                            <option value="">请选择</option>\n' +
                    '                            <option value="01" title="教育部">教育部</option>\n' +
                    '                            <option value="02" title="区级">区级</option>\n' +
                    '                        </select>\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '            </div>\n' +
                    '            <div class="layui-form-item">\n' +
                    '                <div class="layui-inline">\n' +
                    '                    <label class="layui-form-label">项目级别</label>\n' +
                    '                    <div class="layui-input-inline">\n' +
                    '                        <select name="lev"  lay-search="">\n' +
                    '                            <option value="">请选择</option>\n' +
                    '                            <option value="01">国际级</option>\n' +
                    '                            <option value="02">国家级</option>\n' +
                    '                            <option value="03">部省级</option>\n' +
                    '                            <option value="04">省专项</option>\n' +
                    '                            <option value="05">厅市级</option>\n' +
                    '                            <option value="06">其他</option>\n' +
                    '                        </select>\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '            </div>\n' +
                    '            <div class="layui-form-item">\n' +
                    '                <div class="layui-inline">\n' +
                    '                    <label class="layui-form-label">项目分类</label>\n' +
                    '                    <div class="layui-input-inline">\n' +
                    '                        <select name="category" lay-search="">\n' +
                    '                            <option value="">请选择</option>\n' +
                    '                            <option value="01">工类</option>\n' +
                    '                            <option value="02">社科类</option>\n' +
                    '                            <option value="03">其它</option>\n' +
                    '                        </select>\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '            </div>\n' +
                    '            <div class="layui-form-item">\n' +
                    '                <div class="layui-inline">\n' +
                    '                    <div class="layui-form-item layui-form-text">\n' +
                    '                        <span style="color:red">*</span><label class="layui-form-label">计划描述</label>\n' +
                    '                        <div class="layui-input-inline">\n' +
                    '                            <textarea placeholder="请输入内容" class="layui-textarea des required"  name="des"></textarea>\n' +
                    '                        </div>\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '            </div>\n' +
                    '            <div class="layui-form-item">\n' +
                    '                <div class="layui-inline">\n' +
                    '                    <span style="color:red">*</span><label class="layui-form-label">计划创建日期</label>\n' +
                    '                    <div class="layui-input-inline">\n' +
                    '                        <input type="text" name="createTime" id="creatdate" lay-verify="date"\n' +
                    '                               autocomplete="off" class="layui-input required">\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '            </div>\n' +
                    '            <div class="layui-form-item">\n' +
                    '                <div class="layui-inline">\n' +
                    '                    <span style="color:red">*</span><label class="layui-form-label">开始日期</label>\n' +
                    '                    <div class="layui-input-inline">\n' +
                    '                        <input type="text" name="beginDate" id="date" lay-verify="date"\n' +
                    '                               autocomplete="off" class="layui-input required">\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '            </div>\n' +
                    '            <div class="layui-form-item">\n' +
                    '                <div class="layui-inline">\n' +
                    '                    <span style="color:red">*</span><label class="layui-form-label">结束日期</label>\n' +
                    '                    <div class="layui-input-inline">\n' +
                    '                        <input type="text" name="endDate" id="date1" lay-verify="date"\n' +
                    '                               autocomplete="off" class="layui-input required">\n' +
                    '                    </div>\n' +
                    '                </div>\n' +
                    '            </div>\n' +
                    '            <div class="layui-form-item">\n' +
                    '                <label class="layui-form-label">查询显示</label>\n' +
                    '                <div class="layui-input-inline">\n' +
                    '                    <input type="radio" name="display" value="1" title="不显示" checked="">\n' +
                    '                    <input type="radio" name="display" value="2" title="显示">\n' +
                    '                </div>\n' +
                    '            </div>\n' +
                    '            <div class="layui-form-item">\n' +
                    '                <label class="layui-form-label">计划状态</label>\n' +
                    '                <div class="layui-input-inline">\n' +
                    '                    <input type="radio" name="state" value="1" title="申报中" checked="">\n' +
                    '                    <input type="radio" name="state" value="2" title="申报结束">\n' +
                    '                </div>\n' +
                    '            </div>\n' +
                    ' <div class="layui-form-item"  style="margin-top:15px">\n' +
                    '    <label class="layui-form-label" style="width: 100px;margin-left:-20px">上传附件</label>\n' +
                    '    <div class="layui-input-inline">\n' +
                    '<div id="fujians"></div>' +
                    ' <div id="fileAll">\n' +
                    '</div>\n' +
                    '<a href="javascript:;" class="openFile" style="float: left;margin-top:8px;position:relative">\n' +
                    '<img src="../img/mg11.png" alt="">\n' +
                    '<span>添加附件</span>\n' +
                    '<input type="file" multiple id="fileupload" data-url="/upload?module=fixAssets" name="file">\n' +
                    '</a>\n' +
                    '</div>\n' +
                    '</div>' +
                    '        </form>\n' +
                    '    </div>',
                success:function (res) {
                    //日期
                    laydate.render({
                        elem: '#date'
                        , trigger: 'click'//呼出事件改成click
                    });
                    laydate.render({
                        elem: '#date1'
                        , trigger: 'click'//呼出事件改成click
                    });
                    laydate.render({
                        elem: '#creatdate'
                        ,trigger: 'click'//呼出事件改成click
                        ,type:'datetime'
                        ,format: 'yyyy-MM-dd HH:mm:ss'
                    });
                    form.render();
                    fileuploadFn('#fileupload', $('#fileAll'));
                    if(type==1){
                        $.ajax({
                            url:'/rpmPlan/selectByKey',
                            dataType: 'json',
                            type: 'get',
                            data:{
                                planId:planId
                            },
                            success:function (res) {
                                var str = ''
                                form.render()
                                if(res.object.attachmentList.length>0){
                                    for(var i=0;i<res.object.attachmentList.length;i++){
                                        str+='<div class="dech" deUrl="' +res.object.attachmentList[i].attUrl + '"><a href="/download?' + res.object.attachmentList[i].attUrl + '" NAME="' + res.object.attachmentList[i].attachName +'*"><img style="margin-right:10px;" src="/img/attachment_icon.png"/>' + res.object.attachmentList[i].attachName + '</a><img class="deImgs" style="margin-left:5px;cursor: pointer;" src="/img/file/icon_deletecha_03.png"/><input type="hidden" class="inHidden" value="' + res.object.attachmentList[i].aid + '@' + res.object.attachmentList[i].ym + '_' + res.object.attachmentList[i].attachId +',"></div>'
                                    }
                                }else{
                                    str='';
                                }
                                $('#fujians').html(str)
                                form.val("ajaxforms", res.object);
                            }

                        })
                    }
                },
                yes:function (index) {
                    for (var i = 0; i < $('.required').length; i++) {
                        if ($('.required').eq(i).val() == '') {
                            var content=$('.required').eq(i).parents('.layui-inline').children('.layui-form-label').text()
                            if(content==''){
                                var content=$('.required').eq(i).parents('.layui-form-item').children('.layui-form-label').text()
                            }
                            $.layerMsg({
                                content: '' + content + '不能为空',
                                icon: 2
                            });
                            return false;
                        }
                    }
                    var obj = {}
                    //附件
                    var attachmentId = '';
                    var attachmentName = '';
                    for (var i = 0; i < $('#fileAll .dech').length; i++) {
                        attachmentId += $('#fileAll .dech').eq(i).find('input').val();
                        attachmentName += $('#fileAll a').eq(i).attr('name');
                    }
                    if(type == 0){
                        var url = '/rpmPlan/insertSelective'
                    }else{
                        var url = '/rpmPlan/updateByKey'
                        obj.planId = planId
                    }
                    obj.planName = $('input[name="planName"]').val()
                    obj.source = $('select[name="source"]').val()
                    obj.lev = $('select[name="lev"]').val()
                    obj.category = $('select[name="category"]').val()
                    obj.des = $('.des').val()
                    obj.beginDate = $('input[name="beginDate"]').val()
                    obj.endDate = $('input[name="endDate"]').val()
                    obj.createTime = $('input[name="createTime"]').val()
                    obj.display = $('input[name="display"]:checked').val()
                    obj.state = $('input[name="state"]:checked').val()
                    obj.attachmentId = attachmentId
                    obj.attachmentName =attachmentName
                    $.ajax({
                        url: url,
                        dataType: 'json',
                        type: 'get',
                        data: obj,
                        success: function (res) {
                            if (res.flag) {
                                layer.msg('新增成功！', {icon: 1});
                                tableInt.reload()
                                layer.closeAll();
                            }
                        }
                    })
                }
            })
        }
        var userId
        //当前登录人
        $.ajax({
            url: '/Meetequipment/getuser',
            type: 'get',
            dataType: 'json',
            success: function (res) {
                userId = res.object.userId
            }
        })
        //监听行工具事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            var planId = data.planId
            if(obj.event === 'edit'){
                creat(1,planId)
            }else if(obj.event === 'del'){
                layer.confirm('真的删除当前数据吗？', function (index) {
                    $.ajax({
                        type: 'get',
                        url: '/rpmPlan/deleteByKey?planId='+planId,
                        dataType: 'json',
                        success: function (res) {
                            if (res.msg == 'ok') {
                                layer.msg('删除成功！', {icon: 1});
                                tableInt.reload()
                                layer.closeAll();
                            }
                        }
                    })
                });
            }else{
                window.location.href='/rpmPlan/rpmDeclaration?planId='+planId+'&id='+userId+'&type='+1;
            }
        });
        //删除附件
        $(document).on('click', '.deImgs', function () {
            var _this = this;
            var attUrl = $(this).parents('.dech').attr('deUrl');
            layer.confirm('确定删除该附件吗？', function (index) {
                $.ajax({
                    type: 'get',
                    url: '/delete?' + attUrl,
                    dataType: 'json',
                    success: function (res) {

                        if (res.flag == true) {
                            layer.msg('删除成功', {icon: 6, time: 1000});
                            $(_this).parent().remove();
                        } else {
                            layer.msg('删除失败', {icon: 2, time: 1000});
                        }
                    }
                })
            });
        });
        //导出
        $('.export').click(function () {
            window.location.href="/rpmPlan/export"
        })
    });
</script>
